package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.DZzxfundsMapper;
import com.hbnu.system.model.entity.DZzxfunds;
import com.hbnu.system.model.entity.Porproject_registration;
import com.hbnu.system.service.DZzxfundsService;
import com.hbnu.system.service.IPorproject_registrationService;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.utils.CheckRoleUtil;
import com.hbnu.system.utils.RegexPatterns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class DZzxfundsServiceImpl extends ServiceImpl<DZzxfundsMapper, DZzxfunds> implements DZzxfundsService {
    @Autowired
    DZzxfundsService dZzxfundsService;
    @Autowired
    IPorproject_registrationService iPorproject_registrationService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private CheckRoleUtil checkRoleUtil;


    @Override
    public boolean addDZzxfundsProject(DZzxfunds dzfunds) {
        //不使用前端传过来的id
        dzfunds.setDzfundsID(null);
        //查询纵向项目
        Porproject_registration project = iPorproject_registrationService.getPorproject(dzfunds.getPregistrationID());
        checkRoleUtil.checkPermissionProject(project.getProjectState());
        if(StrUtil.isEmpty(dzfunds.getPregistrationID()) || StrUtil.isBlank(dzfunds.getPregistrationID())){
            throw new BaseException("请输入纵向项目id!");
        }
        Porproject_registration tr = iPorproject_registrationService.getOne(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getPRegistrationID, dzfunds.getPregistrationID()));
        if(Objects.isNull(tr)){
            throw new BaseException("纵向项目不存在!");
        }
        if(StrUtil.isEmpty(dzfunds.getDzfundsNum()) || StrUtil.isBlank(dzfunds.getDzfundsNum())){
            throw new BaseException("请输入到账编号!");
        }
        DZzxfunds dZfunds = dZzxfundsService.getOne(Wrappers.lambdaQuery(DZzxfunds.class)
                .eq(DZzxfunds::getDzfundsNum,dzfunds.getDzfundsNum()));
        if(!Objects.isNull(dZfunds)){
            throw new BaseException("到账编号已被使用");
        }
        if(StrUtil.isEmpty(dzfunds.getFundsSours()) || StrUtil.isBlank(dzfunds.getFundsSours())){
            throw new BaseException("请输入经费来源!");
        }
        if(StrUtil.isEmpty(dzfunds.getDzfundsAmount()) || StrUtil.isBlank(dzfunds.getDzfundsAmount())){
            throw new BaseException("请输入到账金额!");
        }
        //校验经费是否输入的是数字
        if (!dzfunds.getDzfundsAmount().matches(RegexPatterns.NUMBER_REGEX)) {
            throw new BaseException("经费请输入数字");
        }
        if(Objects.isNull(dzfunds.getDzfundsDate())){
            throw new BaseException("请输入到账时间!");
        }
        float assessment = Float.parseFloat(dzfunds.getDzfundsAmount());
        float num = tr.getDzFunds()+assessment;
        tr.setDzFunds(num);
        UpdateWrapper<Porproject_registration> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PRegistrationID",tr.getPRegistrationID());
        iPorproject_registrationService.update(tr,updateWrapper);
        dzfunds.setUploadTime(LocalDateTime.now());
        return baseMapper.insert(dzfunds)==1;
    }

    @Override
    public boolean updateDZzxfundsProject(DZzxfunds dzfunds) {
        Porproject_registration project = iPorproject_registrationService.getPorproject(dzfunds.getPregistrationID());
        checkRoleUtil.checkPermissionProject(project.getProjectState());
        if(StrUtil.isEmpty(dzfunds.getPregistrationID()) || StrUtil.isBlank(dzfunds.getPregistrationID())){
            throw new BaseException("请输入纵向项目id!");
        }
        //校验经费是否输入的是数字
        if (!dzfunds.getDzfundsAmount().matches(RegexPatterns.NUMBER_REGEX)) {
            throw new BaseException("经费请输入数字");
        }
        Porproject_registration tr = iPorproject_registrationService.getOne(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getPRegistrationID, dzfunds.getPregistrationID()));
        if(Objects.isNull(tr)){
            throw new BaseException("纵向项目不存在!");
        }
        if(StrUtil.isEmpty(dzfunds.getDzfundsNum()) || StrUtil.isBlank(dzfunds.getDzfundsNum())){
            throw new BaseException("请输入到账编号!");
        }
        DZzxfunds dZfunds = dZzxfundsService.getOne(Wrappers.lambdaQuery(DZzxfunds.class)
                .eq(DZzxfunds::getDzfundsNum,dzfunds.getDzfundsNum())
                .ne(DZzxfunds::getDzfundsID,dzfunds.getDzfundsID()));
        if(!Objects.isNull(dZfunds)){
            throw new BaseException("到账编号已被使用");
        }
        if(StrUtil.isEmpty(dzfunds.getFundsSours()) || StrUtil.isBlank(dzfunds.getFundsSours())){
            throw new BaseException("请输入经费来源!");
        }
        if(StrUtil.isEmpty(dzfunds.getDzfundsAmount()) || StrUtil.isBlank(dzfunds.getDzfundsAmount())){
            throw new BaseException("请输入到账金额!");
        }
        if(Objects.isNull(dzfunds.getDzfundsDate())){
            throw new BaseException("请输入到账时间!");
        }
        DZzxfunds dZfunds1 = dZzxfundsService.getOne(Wrappers.lambdaQuery(DZzxfunds.class)
                .eq(DZzxfunds::getDzfundsID,dzfunds.getDzfundsID()));
        float num1 = Float.parseFloat(dZfunds1.getDzfundsAmount());
        float assessment = Float.parseFloat(dzfunds.getDzfundsAmount());
        float num = tr.getDzFunds()+assessment-num1;
        tr.setDzFunds(num);
        UpdateWrapper<Porproject_registration> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PRegistrationID",tr.getPRegistrationID());
        iPorproject_registrationService.update(tr,updateWrapper);
        dzfunds.setUploadTime(LocalDateTime.now());
        return baseMapper.updateById(dzfunds)==1;
    }

    @Override
    public boolean removeDZzxfundsProject(String dzfundsID) {
        DZzxfunds dZfunds = super.getOne(Wrappers.lambdaQuery(DZzxfunds.class)
                .eq(DZzxfunds::getDzfundsID,dzfundsID));
        if(Objects.isNull(dZfunds)){
            throw new BaseException("不存在该到账经费");
        }
        Porproject_registration tr = iPorproject_registrationService.getOne(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getPRegistrationID, dZfunds.getPregistrationID()));
        if(Objects.isNull(tr)){
            throw new BaseException("纵向项目不存在!");
        }
        checkRoleUtil.checkPermissionProject(tr.getProjectState());
        float num1 = Float.parseFloat(dZfunds.getDzfundsAmount());
        float num = tr.getDzFunds()-num1;
        tr.setDzFunds(num);
        UpdateWrapper<Porproject_registration> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PRegistrationID",tr.getPRegistrationID());
        iPorproject_registrationService.update(tr,updateWrapper);

        return baseMapper.deleteById(dZfunds) == 1;


    }

    @Override
    public List<DZzxfunds> getDZzxfundsByPorprojectID(String porprojectID) {
        Porproject_registration tranproject_registration = iPorproject_registrationService.getById(porprojectID);
        if(Objects.isNull(tranproject_registration)){
            throw new BaseException("输入的纵向项目编号有误");
        }
        List<DZzxfunds> dzfunds = baseMapper.selectList(Wrappers.lambdaQuery(DZzxfunds.class)
                .eq(DZzxfunds::getPregistrationID,porprojectID));
        return dzfunds;
    }


}
